package com.renatn.yach.dao.jpa;

import com.renatn.yach.dao.YachListDao;
import com.renatn.yach.domain.YachList;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * User: rnasyrov
 * Date: 10.01.12
 * Time: 15:00
 */

@Repository("yachListDao")
public class YachListDaoJpa extends GenericDaoJpa<YachList> implements YachListDao {

    public YachListDaoJpa() {
        super(YachList.class);
    }

    @Override
    public List<YachList> getByName(String name) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<YachList> criteriaQuery = criteriaBuilder.createQuery(YachList.class);
        Root<YachList> root = criteriaQuery.from(YachList.class);
        Path<String> path = root.<String>get("name");
        criteriaQuery.where(criteriaBuilder.equal(path, name));
        return entityManager.createQuery(criteriaQuery).getResultList();
    }
}
